package cn.tedu.collection;/**
 * @Author 金帮嘉诚 @Date 2021-06-22 星期二 10:27
 */
/*本类用于测试 Collection相关API*/

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * 知识点
 * collection定义的方法
 * 集合中的方法 增删
 * add
 * remove
 * contains
 * hashCode
 * isEmpty
 * size
 * equal
 * toArray
 * clear
 * 集合间的方法
 * addAll
 * containsAll
 * removeAll
 * retainAll
 * 迭代器
 * iterator
 */
public class TestCollection {
    public static void main(String[] args) {
        Collection<Integer> collection = new ArrayList<>();
        collection.add(1);
        collection.add(2);
        collection.add(3);
        collection.add(4);
        collection.add(5);
        collection.add(6);
        collection.add(2);
        collection.add(2);

        System.out.println(collection.remove(2));
        System.out.println(collection);
        System.out.println(collection.contains(3));
        System.out.println(collection.hashCode());

        System.out.println(collection.isEmpty());
        System.out.println("collection.size() = " + collection.size());
        System.out.println("collection.equals(3) = " + collection.equals(3));
        //注意这个方法是返回对象数组
        Object[] array = collection.toArray();
        String s = Arrays.toString(array);
        System.out.println("s = " + s);

        Collection<Integer> collection1=new ArrayList<>();
        collection1.add(300);
        collection1.add(200);
        collection1.add(300);
        collection1.add(400);
        collection1.add(500);

        collection.addAll(collection1);
        System.out.println("collection.containsAll(collection1) = " + collection.containsAll(collection1));
        System.out.println(collection);
        System.out.println("collection.removeAll(collection1) = " + collection.removeAll(collection1));
        System.out.println(collection);
        System.out.println("collection.retainAll(collection1) = " + collection.retainAll(collection1));
        System.out.println(collection);
        collection.addAll(collection1);

        Iterator<Integer> iterator = collection.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}
